<template>
  <div>
    <el-dialog title="投料配置" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }">

        <el-row v-for="item in boxsList" :key="item.id">
          <el-col :span="4">
            <div style="line-height: 2.7rem;">鱼池编号：{{ item.fishpondName }}</div>
          </el-col>
          <el-col :span="10">
            <el-form-item label="总投量：">
              <el-input v-model="item.feedingTotal" placeholder="请输入总投量" clearable show-word-limit :disabled="seeType">
                <i slot="suffix" class="dw">(kg)</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="投喂量：">
              <el-input v-model="item.feedingAmount" placeholder="请输入投喂量" clearable show-word-limit :disabled="seeType">
                <i slot="suffix" class="dw">(kg)</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import md5 from "js-md5";
import { getConfigAutoTaskGroupFeedList, getSysCompanyBases, saveConfigAutoTaskGroupFeed, getObjBoxs } from "@/api/system";
import { getObjFishponds } from "@/api/fishpond";
import { getCompanyInfoTree } from "@/api/company";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
    type: {
      default: ''
    }
  },
  data() {
    return {
      id: '',
      value: 1,
      ruleForm: {
        // 表单字段
        id: "",
        companyId: "",
        baseId: "",
        name: "",
        ids: "",
        orderNum: "",
        taskTime: "",
      },
      ids: '',
      names: '',
      boxsList: [],
      fishList: [],
      rules: {
        // 表单规则
        name: [{ required: true, message: "请输入分组名称", trigger: "blur" }],
        orderNum: [{ required: true, message: "请输入排序号", trigger: "blur" }],
        taskTime: [{ required: true, message: "请输入任务时长", trigger: "blur" }],
        companyId: [{ required: true, message: "请选择公司名称", trigger: "blur" }],
        baseId: [{ required: true, message: "请选择基地名称", trigger: "blur" }],
        ids: [{ required: true, message: "请选择区域", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      baseArray: [], // 角色集合
      companyArray: [], // 公司集合
      userType: "", // 用户类型（0超级管理员，1公司管理员）
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm = {}
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        this.userType = Number(window.localStorage.getItem("userType"));

        // 获取公司列表
        this.getCompanyInfoTree();

        this.getSysCompanyBases();
      }
    },
  },
  methods: {

    getids(id) {
      getConfigAutoTaskGroupFeedList({ id: id }).then((res) => {
        if (res.code == 200) {
          this.boxsList = res.data.data
          let id = this.ids.split(',')
          id.map(item => Number(item))
          let names = this.names.split(',')
          names.map(item => Number(item))
          let list = []
          let arr = []
          let a = 1
          id.map((item, index) => {
            list.push({
              fishpondName: names[index],
              fishpondId: Number(item),
              feedingTotal: '',
              groupId: this.id,
              feedingAmount: ''
            }
            )
          })
          this.boxsList.map(item => {
            list.map((eitem, index) => {
              if (item.fishpondName == eitem.fishpondName) {
                list[index] = item
              }
            })
          })
          this.boxsList = list

        }
      });

    },
    // 详情
    getDetail() {
      // getConfigAutoTaskGroupFeedList({ id: this.addEditId }).then((res) => {
      //   if (res.code == 200) {
      //     this.ruleForm = res.data;

      //     // 获取公司列表
      //     this.getCompanyInfoTree();

      //     this.getSysCompanyBases();
      //   }
      // });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {

          saveConfigAutoTaskGroupFeed({ list: this.boxsList }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.addEditIsDialog_c = false
              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.addEditIsDialog_c = false
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 获取公司列表
    getCompanyInfoTree() {
      if (this.ruleForm.userType === "") {
        return;
      }

      if (this.ruleForm.userType == 0) {
        // 超级管理员，公司只有一个总公司
        this.companyArray = [
          {
            id: 0,
            name: "总公司",
            parentId: 0,
          },
        ];
      } else {
        getCompanyInfoTree({}).then((res) => {
          if (res.code == 200) {
            this.companyArray = res.data;
          }
        });
      }
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.ruleForm.companyId = val[val.length - 1];
      } else {
        this.ruleForm.companyId = "";
      }

      // this.ruleForm.baseId = "";
      this.baseArray = [];

      // 获取角色列表
      this.getSysCompanyBases();
    },
    // 获取角色列表
    getSysCompanyBases() {
      if (this.ruleForm.companyId === "" || this.ruleForm.companyId == null) {
        return;
      }

      getSysCompanyBases({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.baseArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}

.el-cascader {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
